package com.richard.base.net.api

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/***
 * @Author wuzhiguo
 *
 * 基础 retrofit client
 *
 */
abstract class BaseRetrofitClient {

    private val client: OkHttpClient by lazy {
        val builder = OkHttpClient.Builder()
        handleBuilder(builder)
        builder.build()
    }

    private val retrofit:Retrofit by lazy { Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addCallAdapterFactory(CoroutineCallAdapterFactory.invoke())
            .baseUrl(baseUrl())
            .build() }

    protected abstract fun handleBuilder(builder: OkHttpClient.Builder)
    protected abstract fun baseUrl():String

    fun <S> getService(serviceClass: Class<S>): S {
        return retrofit.create(serviceClass)
    }
}
